//
//  AXFScaniconViewController.m
//  AIXF
//
//  Created by Macx on 2017/7/10.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AXFScaniconViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <Masonry.h>
#import "UIView+WP.h"
@interface AXFScaniconViewController ()<AVCaptureMetadataOutputObjectsDelegate>


@property (strong, nonatomic)   AVCaptureInput *videoInput;

@property (strong, nonatomic)   AVCaptureSession *session;

@property (strong, nonatomic)   AVCaptureMetadataOutput *metaOutput;

@property (nonatomic,strong)UIImageView *yellowlight;

@property (nonatomic ,strong)UIImageView *imageView;
@end

@implementation AXFScaniconViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
 
    if (TARGET_IPHONE_SIMULATOR) {
    
        self.view.backgroundColor = [UIColor darkGrayColor];
        
        self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pick_bg"]];
        
        [self.view addSubview:self.imageView];
//        
//        imageView.x = 85;
//        
//        imageView.y = 150;
//        imageView.width = 220;
//        imageView.height = 220;
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.centerY.equalTo(self.view);
            make.width.offset(180);
            make.height.offset(180);
        }];
        
       
        
        UIImageView *yellowlight = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yellowlight"]];
        
        [self.view addSubview:yellowlight];
        
        _yellowlight = yellowlight;
        
        [yellowlight mas_makeConstraints:^(MASConstraintMaker *make) {
            
           
            make.top.equalTo(self.imageView.mas_top);
            make.left.equalTo(self.imageView.mas_left);
            make.width.offset(180);
            make.height.offset(20);

        }];
        
//        yellowlight.x = 80;
//        
//        yellowlight.y = 150;
        
        CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"position.y"];
        
        basic.toValue = @360;
        
        basic.duration = 1.5;
        
        basic.repeatCount = INT_MAX;
        
        [_yellowlight.layer addAnimation:basic forKey:nil];
        
        
    }else{
        
        
        AVCaptureDevice *videoVideo = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        
        self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoVideo error:nil];
        
        self.metaOutput = [AVCaptureMetadataOutput new];
        
        self.session = [AVCaptureSession new];
        
        if([self.session canAddInput:self.videoInput]) {
            [self.session addInput:self.videoInput];
        }
        
        if([self.session canAddOutput:self.metaOutput]) {
            [self.session addOutput:self.metaOutput];
        }

        self.metaOutput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
        
        [self.metaOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
        
        AVCaptureVideoPreviewLayer *previewPlayer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
        previewPlayer.frame = self.view.bounds;
        
        [self.view.layer addSublayer:previewPlayer];
        
        [self.session startRunning];
    }
   
}

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
//    for (AVMetadataMachineReadableCodeObject *obj in metadataObjects) {
//        NSLog(@"%@",obj.stringValue);
//        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:obj.stringValue]];
//    }
}

@end
